import { uploadOssInfo } from '@/api/common'
import axios from 'axios'
var accessid = ''
var accesskey = ''
var host = ''
var policyBase64 = ''
var signature = ''
var callbackbody = ''
var filename = ''
var key = ''
var expire = 0
var now = Date.parse(new Date()) / 1000
var timestamp = Date.parse(new Date()) / 1000
var finallName = ''
let mediaType = ''
function random_string(len) {
  len = len || 32
  var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
  var maxPos = chars.length
  var pwd = ''
  for (let i = 0; i < len; i++) {
    pwd += chars.charAt(Math.floor(Math.random() * maxPos))
  }
  return Date.parse(new Date()) / 1000 + '_' + pwd
}

function get_suffix(filename) {
  var pos = filename.lastIndexOf('.')
  var suffix = ''
  if (pos != -1) {
    suffix = filename.substring(pos)
  }
  return suffix
}
async function uploadData(file, type) {
  file.type.indexOf('video') !== -1 ? mediaType = 'zhangyu-priv' : mediaType = 'zhangyu-pub'
  // 指定传到公共空间
  file.mediaType ? mediaType = 'zhangyu-pub' : ''
  // zhangyu-priv 私有空间用来储存短剧视频 zhangyu-pub 公共空间 用来存放图片等
  console.log(mediaType, 'mediaType')
  var now = Date.parse(new Date()) / 1000
  var timestamp = Date.parse(new Date()) / 1000
  // if (expire < now + 3) {
  const res = await uploadOssInfo({ bucket: mediaType })
  if (res.status_code === 1) {
    var obj = res.data
    host = obj['host']
    policyBase64 = obj['policy']
    accessid = obj['accessid']
    signature = obj['signature']
    expire = parseInt(obj['expire'])
    // callbackbody = obj['callback']
    key = obj['dir']
  }
  var suffix = get_suffix(file.name)

  const autoname = random_string(10)
  const finallName = key + autoname + suffix
  console.log(key, autoname, suffix, '文件名称')
  // 组装发送数据
  var request = new FormData()
  request.append('name', file.name)
  request.append('key', finallName) // 文件名字，可设置路径
  request.append('policy', policyBase64) // policy规定了请求的表单域的合法性
  request.append('OSSAccessKeyId', accessid) // Bucket 拥有者的Access Key Id。
  request.append('success_action_status', '200') // 让服务端返回200,不然，默认会返回204
  // request.append('callback', callbackbody) //回调，非必选，可以在policy中自定义
  request.append('Signature', signature) // 根据Access Key Secret和policy计算的签名信息，OSS验证该签名信息从而验证该Post请求的合法性
  request.append('file', file) // 需要上传的文件 file
  const response = await axios({
    url: host,
    data: request,
    processData: false, // 默认true，设置为 false，不需要进行序列化处理
    cache: false, // 设置为false将不会从浏览器缓存中加载请求信息
    async: false, // 发送同步请求
    contentType: false, // 避免服务器不能正常解析文件---------具体的可以查下这些参数的含义
    dataType: 'json', // 不涉及跨域  写json即可
    method: 'post'
  }).catch(function (error) {
    console.log(error)
    return {
      host,
      finallName,
      status_code: 0,
      type,
      name: file.name,
      suffix: suffix
    }
  })
  if (response.status === 200) {
    return {
      host,
      finallName,
      status_code: 1,
      type,
      name: file.name,
      suffix: suffix
    }
  } else {
    return {
      host,
      finallName,
      status_code: 0,
      type,
      name: file.name,
      suffix: suffix
    }
  }
}
// export default uploading
function _install(Vue) {
  if (Vue && Vue.uploading === undefined) {
    Vue.prototype.uploading = uploadData
  } else {
    return
  }
}
export default _install

